import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:ride_rex/article/post/article_post_page.dart';
import 'package:ride_rex/standard/app_colors.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import '../route.dart';
import '../service/log_service.dart';

//主tab框架
class CommonHomeScaffold extends StatelessWidget {
  CommonHomeScaffold(
    this.controller,
    this.items,
    this.onPageChanged,
    this.checkedColor,
    this.normalColor, {
    this.physics,
    Key? key,
  }) : super(key: key);

  final CommonHomeScaffoldController controller;
  final List<CommonHomeScaffoldItem> items;
  final ValueChanged<int> onPageChanged;
  final Color checkedColor;
  final Color normalColor;
  final ScrollPhysics? physics;

  final double safeBottom = ScreenUtil().bottomBarHeight * 0.5;
  static final double itemsHeight = 60.w;
  final double top = -14.w;

  @override
  Widget build(BuildContext context) {
    var content = PageView(
      physics: physics ?? const NeverScrollableScrollPhysics(),
      controller: controller._pageController,
      onPageChanged: (page) {
        controller._curPage = page;
        controller._refreshTab();
        onPageChanged(page);
        LMLogManager.stat(page: 'tab-${items[page].name}');
      },
      children: items.map((e) => e.page).toList(),
    );

    return Stack(
      alignment: Alignment.bottomCenter,
      children: [
        content.paddingBottom(itemsHeight + safeBottom),
        _buildBottomBar(),
      ],
    );
  }

  ///tabBar
  Widget _buildBottomBar() {
    return SizedBox(
      height: safeBottom + itemsHeight,
      child: ValueListenableBuilder(
        valueListenable: controller._tabController,
        builder: (context, value, Widget? child) {
          List<Widget> children = items.map((e) => _item(e)).toList();
          children.insert(2, const Spacer());
          return Container(
            height: safeBottom + itemsHeight,
            decoration: CommonDecoration.getRadius(topLeft: 18, topRight: 18, bg: Colors.white),
            padding: EdgeInsets.only(bottom: safeBottom),
            child: Stack(
              alignment: Alignment.center,
              children: [
                Row(children: children),
                _buildAddBtn(context),
              ],
            ),
          );
        },
      ),
    );
  }

  Widget _item(CommonHomeScaffoldItem item) {
    double w = (ScreenUtil().screenWidth / 5.0).floorToDouble();
    bool selected = controller.curPage == items.indexOf(item);
    Color color = selected ? checkedColor : normalColor;
    String icon = selected ? item.selectedIcon : item.icon;
    Widget btn = ButtonWidget(
      onTap: () => controller._pageController.jumpToPage(items.indexOf(item)),
      alignment: Alignment.center,
      hasInkWell: false,
      width: w,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SvgPicture.asset(icon, width: 24, height: 24),
          const Padding(padding: EdgeInsets.only(top: 3)),
          Text(item.name, style: TextStyle(fontSize: 11, color: color))
        ],
      ),
    );
    return btn;
  }

  Widget _buildAddBtn(BuildContext context) {
    return ButtonWidget(
      onTap: () => CommonRoute.open(const ArticlePostPage()),
      hasInkWell: false,
      width: 38,
      height: 38,
      borderRadius: 12,
      gradient: AppColors.primaryGradient,
      child: const Icon(Icons.add, size: 34, color: Colors.white),
    );
  }
}

class CommonHomeScaffoldController {
  CommonHomeScaffoldController(int initialPage)
      : _curPage = initialPage,
        _pageController = PageController(initialPage: initialPage);

  final PageController _pageController;
  final ValueNotifier<bool> _tabController = ValueNotifier(false);
  int _curPage;

  int get curPage => _curPage;

  void refreshBadge() => _refreshTab();

  void _refreshTab() {
    _tabController.value = !_tabController.value;
  }

  void changeIndex(int index) => _pageController.jumpToPage(index);
}

class CommonHomeScaffoldItem {
  CommonHomeScaffoldItem(this.page, this.name, this.icon, this.selectedIcon, this.hasPoint, [this.logic]);

  Widget page;
  String name;
  String icon;
  String selectedIcon;
  String Function() hasPoint;
  dynamic logic;
}
